package com.zhiyin.game.Service;

import com.alibaba.fastjson.JSONObject;
import com.zhiyin.game.bean.Vector2;
import com.zhiyin.game.utils.RedisTemplateUtils;
import com.zhiyin.game.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;

@Component
public class WalkExecuteService {

    private final String redisKey = "zhiyin.game";


    @Autowired
    private WalkingService walkingService;

    public void doWalk(Map<String, Object> mapInfo) throws InterruptedException {
        List<Vector2> list = walkingService.startWalking(10, 10, 3, "thief", mapInfo);
        Stack<Map> stack = new Stack();
        for (int i = 1; i < list.size(); i++) {
            if (RedisTemplateUtils.existHashKey(redisKey,"walk")){
                String walk = RedisTemplateUtils.getHashValue(redisKey, "walk").toString();
                if (walk.equals("0")){
                    break;
                }
            }
            Vector2 vector2 = list.get(i);
            Map<String, Object> message = new HashMap<>();
            Map<String, Integer> point = new HashMap<>();
            message.put("role", "thief");
            point.put("x", vector2.getX());
            point.put("y", vector2.getY());
            if (!stack.empty()){
                message.put("lastpoint",stack.pop());
            }else {
                message.put("lastpoint",new HashMap<>());
            }
            stack.push(point);
            message.put("point", point);
            message.put("count", i);
            WebSocketServer.sendMessage(message);
            Thread.sleep(1000);
        }
        Map<String,Object> endMessage = new HashMap<>();
        endMessage.put("over","true");
        endMessage.put("point",stack.pop());
        WebSocketServer.sendMessage(endMessage);
    }
}
